//
//  YMLeaseVC.swift
//  NewVersionDemo
//
//  Created by zeng on 2018/3/14.
//  Copyright © 2018年 youmy. All rights reserved.
//  租机

import UIKit

class YMLeaseVC: UIViewController {

    var tableView: UITableView?
    
    var isOffset:Bool = false
    
    lazy var headerView = YMLeaseHeaderView.nibView()
    
    lazy var sectionHeaderView = YMLeaseSectionHeaderView.nibView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        tableView = UITableView(frame: ym_screen_bounds)
        tableView?.backgroundColor = ym_bg_color
        view.addSubview(tableView!)
        
        if #available(iOS 11.0, *) {
            tableView?.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        
        tableView?.tableHeaderView = headerView
        tableView?.dataSource = self
        tableView?.delegate = self
        
        tableView?.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "kCellId")
        refresh()
    }

}

// MARK: - 下拉、上拉刷新
extension YMLeaseVC {
    func refresh(){
        
        tableView?.mj_header = YMRefresh.headerRefresh(self, #selector(self.lonaNew))
        
//        tableView?.mj_footer = YMRefresh.footerRefresh(self, #selector(self.loadMore))
    }
    
    @objc func lonaNew(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
            self.tableView?.mj_header.endRefreshing()
        })
    }
    
    @objc func loadMore(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
            self.tableView?.mj_footer.endRefreshingWithNoMoreData()
        })
    }
}

extension YMLeaseVC: UITableViewDelegate, UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "kCellId", for: indexPath)
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let v = UIView(frame: CGRect(x: 0, y: 0, width: ym_screen_width, height: 44))
        v.backgroundColor = .red
        return sectionHeaderView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return ym_screen_height
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.y >= 275 {
            sectionHeaderView.collectionView.isScrollEnabled = true

            if isOffset {
                return
            }else{
                scrollView.contentInset = UIEdgeInsetsMake(scrollView.contentOffset.y, 0, 0, 0);
                isOffset = true
            }
        }else{
            sectionHeaderView.collectionView.isScrollEnabled = false

            if isOffset == false {
                return
            }else{
                scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
                isOffset = false
            }
        }
    }
}



